package com.hhh.petadopt.serviceImpl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hhh.petadopt.mapper.AdminSentMapper;
import com.hhh.petadopt.pojo.Sentp;
import com.hhh.petadopt.pojo.SentpVo;
import com.hhh.petadopt.service.adminSentService;
import com.hhh.petadopt.utils.IDUtils;
import com.hhh.petadopt.utils.Page;


/**
 * 送养管理
 * @author 杜镇声声
 *
 */
@Service
public class adminSentServiceImpl implements adminSentService {
	
	@Autowired
	private AdminSentMapper adminsentmapper;

	//展示送养页面和分页
	@Override
	public Page<Sentp> selectSents(SentpVo Vo) {
		//创建page对象
		Page<Sentp> page = new Page<Sentp>();
		/*
		 * 封装page对象
		 */
		//封装当前页显示的条数
		page.setSize(10);
		Vo.setSize(10);
		if(null != Vo) {
			//设置当前页
			if(null != Vo.getPage()) {
				page.setPage(Vo.getPage());
				//设置起始页
				Vo.setStartRow((Vo.getPage()-1)*Vo.getSize());
			}
			if(null != Vo.getSsname() && !"".equals(Vo.getSsname().trim())) {
				Vo.setSsname(Vo.getSsname().trim());
				
			}
			if(null != Vo.getSsage() && !"".equals(Vo.getSsage().trim())) {
				Vo.setSsage(Vo.getSsage().trim());
				
			}
			if(null != Vo.getSsdate() && !"".equals(Vo.getSsdate())) {
				Vo.setSsdate(Vo.getSsdate());
				
			}
			//性别
			if(null != Vo.getSssex() && !"".equals(Vo.getSssex())) {
				Vo.setSssex(Vo.getSssex().trim());
			}
			//封装符合条件的条数
			page.setTotal(adminsentmapper.selecTotal(Vo));
			//封装符合添加的当前页内容
			List<Sentp> sentList = adminsentmapper.selectSentByVo(Vo);
			List<Sentp> slist = new ArrayList<Sentp>();
			for (Sentp sentp : sentList) {
				sentp.setCreated(sentp.getCreated().substring(0, 10));
				slist.add(sentp);
			}
			
			page.setRows(slist);
		}
		return page;
	}

	//页面信息回显
	@Override
	public Sentp selectSentpById(Integer id) {
		Sentp sentp = adminsentmapper.selectSentpById(id);
		sentp.setSdesc(adminsentmapper.selectDescById(id));
		return sentp;
	}

	//将送养动物信息入库
	@Override
	public void savePett(Sentp sentp) {
		
		/*
		 * 补齐信息
		 */
		//设置上架状态,1表示上架,2表示下架,设置默认上架
		sentp.setSts("1");
		//设置日期
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-mm-dd");
		Date date=new Date();
		String dateStr = sdf.format(date);
		try {
			Date mydate = sdf.parse(dateStr);
			sentp.setCreatedd(mydate);
			
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		sentp.setUpdated(new Date());
		
		
		//删除送来动物的信息
		adminsentmapper.deletesent(sentp.getId());
		adminsentmapper.deleteSentDes(sentp.getId());
		
		//入库
		long ids = IDUtils.genItemId();
		String id = String.valueOf(ids).substring(6);
		sentp.setId(Long.valueOf(id));
		
		adminsentmapper.saveDesc(sentp);
		adminsentmapper.storagepett(sentp);
		/*//发邮件
		String emailMsg = sentp.getSman()+",您好! 当你收到此通知时,您送养的宠物"+sentp.getSname()+",编号:"+sentp.getId()+"已经被接收。"
				+"如有疑问请电话咨询:"+"123456";
		try {
			MailUtils.sendMail(sentp.getSemail(), emailMsg);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		*/
		
	}

	//根据id 删除送养信息
	@Override
	public void deleteSentById(Integer id) {
		adminsentmapper.deleteSentById(id);
		adminsentmapper.deleteDescById(id);
		
	}

	//删除多选
	@Override
	public void deleteSentsByIds(Integer[] ids) {
		//执行操作
		adminsentmapper.deleSentssByIds(ids);
		adminsentmapper.deleteDescByIds(ids);
		
	}

}
